using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 土豆地雷攻击策略 - 检测僵尸踩到后爆炸造成范围伤害
/// </summary>
public class PotatoMineAttackStrategy : AttackStrategyBase
{
    private PotatoMineSkillData m_PotatoMineData;

    public PotatoMineAttackStrategy(SkillData data)
    {
        AttackData = data;
        if (data is PotatoMineSkillData potatoMineData)
        {
            m_PotatoMineData = potatoMineData;
        }
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);
        
        // 当切换到攻击状态时，触发爆炸
        // 这通常发生在僵尸踩到地雷时，PotatoMine 类会切换到攻击状态
        if (Owner != null && Owner is PotatoMine potatoMine)
        {
            // 检查是否已经爆炸，避免重复触发
            if (!potatoMine.HasExploded)
            {
                // 先设置已爆炸标志，避免重复触发
                potatoMine.SetExploded();
                // 然后触发爆炸
                TriggerExplosion();
            }
            else
            {
                Logger.Log("PotatoMineAttackStrategy", $"土豆地雷 {Owner.name} 已经爆炸过，不再重复触发");
            }
        }
        else
        {
            // 土豆地雷是被动触发，不需要主动攻击
            Logger.Log("PotatoMineAttackStrategy", $"土豆地雷 {Owner?.name} 已激活，等待僵尸踩到");
        }
    }

    public override bool CheckCondition()
    {
        // 土豆地雷只要存活且已激活就可以工作
        return Owner != null && Owner.HealthComponent != null && Owner.HealthComponent.CurHealth > 0;
    }

    /// <summary>
    /// 触发爆炸，对范围内的僵尸造成伤害
    /// </summary>
    public void TriggerExplosion()
    {
        if (Owner == null || m_PotatoMineData == null)
        {
            Logger.LogWarning("PotatoMineAttackStrategy", "Owner或技能数据为空，无法触发爆炸");
            return;
        }

        Vector3 explosionCenter = Owner.transform.position;
        float explosionRadius = m_PotatoMineData.ExplosionRadius;
        long explosionDamage = m_PotatoMineData.ExplosionDamage;

        // 使用Physics2D查找范围内的所有碰撞体
        Collider2D[] colliders = Physics2D.OverlapCircleAll(explosionCenter, explosionRadius);
        
        List<Zombie> hitZombies = new List<Zombie>();
        
        foreach (var collider in colliders)
        {
            if (collider == null) continue;
            
            var zombie = collider.GetComponent<Zombie>();
            if (zombie != null && !zombie.HealthComponent.IsDead)
            {
                // 检查距离（确保在爆炸范围内）
                float distance = Vector3.Distance(explosionCenter, zombie.transform.position);
                if (distance <= explosionRadius)
                {
                    hitZombies.Add(zombie);
                }
            }
        }

        // 对范围内的所有僵尸造成伤害
        foreach (var zombie in hitZombies)
        {
            if (zombie != null && !zombie.HealthComponent.IsDead)
            {
                zombie.HealthComponent.TakeDamage(explosionDamage);
                Logger.Log("PotatoMineAttackStrategy", $"{Owner.name} 爆炸对 {zombie.name} 造成 {explosionDamage} 点伤害");
                
                // 如果技能数据有Effects列表，执行效果
                if (AttackData != null && AttackData.Effects != null && AttackData.Effects.Count > 0)
                {
                    SkillExecutor.Execute(AttackData.Effects, Owner, zombie);
                }
            }
        }

        Logger.Log("PotatoMineAttackStrategy", $"{Owner.name} 爆炸命中 {hitZombies.Count} 个僵尸");
    }
}

